﻿#include "systemmainwid.h"
#include "ui_systemmainwid.h"

#include "cfgjsonrw.h"
#include "paramadddialog.h"
#include "robotsettingwid.h"
#include "camerasettingwid.h"
#include "wcssettingwid.h"
#include "safetysettingwid.h"
#include "pipelinesettingwid.h"
#include "BoxlineSettingWid.h"
#include "stackplatesettingwid.h"
#include "unstackplatesettingwid.h"
#include "pathsettingwid.h"
#include "pathaddparamdlg.h"

SystemMainWid::SystemMainWid(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::SystemMainWid())
{
	ui->setupUi(this);
	QHBoxLayout* lay = new QHBoxLayout(ui->groupBox);
	m_plateStackBase = new PositionSettingWid();
	lay->addWidget(m_plateStackBase);


	InitUI();

}

SystemMainWid::~SystemMainWid()
{
	delete ui;
}

void SystemMainWid::InitUI()
{
	InitHWSettingWid();

	InitProject();
	InitBoxline();
	InitStackPlate();
	InitUnstackPlate();
	InitPlateStack();
	InitPipeline();

	//InitPaths();
	InitSafetySettingWid();

}

// 机械手、相机、上位机wcs
void SystemMainWid::InitHWSettingWid()
{
	auto rob = new RobotSettingWid(this);
	rob->Init();
	ui->tabWidget->insertTab(0, rob, "机械手配置");

	auto wcs = new WCSSettingWid(this);

	ui->tabWidget->insertTab(1, wcs, "WCS配置");
	auto camera = new CameraSettingWid(this);
	ui->tabWidget->insertTab(2, camera, "相机配置");
}


// 安全配置
void SystemMainWid::InitSafetySettingWid()
{
	auto wid = new SafetySettingWid(this);
	wid->Init();

	ui->tabWidget->addTab(wid, "安全配置");
}

void SystemMainWid::InitProject()
{
	auto name = cfgrw->GetProjectName();
	ui->lineEdit->setText(name);
}

void SystemMainWid::on_pushButton_clicked()
{
	auto name = ui->lineEdit->text();
	cfgrw->SetProjectName(name);
}

void SystemMainWid::InitBoxline()
{
	auto ids = cfgrw->GetAllBoxlineID();
	for (auto& id : ids) {
		auto cfg = cfgrw->GetBoxlineCfgByID(id);
		auto temp = new BoxlineSettingWid(this);
		temp->Init(cfg);
		ui->tabWidget_4->addTab(temp, id);
	}
}
void SystemMainWid::InitStackPlate()
{
	auto ids = cfgrw->GetAllStackPlateID();
	for (auto& id : ids) {
		auto cfg = cfgrw->GetStackPlateCfgByID(id);
		auto temp = new StackPlateSettingWid(this);
		temp->Init(cfg);
		ui->tabWidget_2->addTab(temp, id);
	}

}

void SystemMainWid::InitPlateStack()
{
	auto cfg = cfgrw->GetPlateStackCfg();
	ui->checkBox->setChecked(cfg.valied() == 1);
	ui->lineEdit_2->setText(cfg.id());
	ui->spinBox->setValue(cfg.minDi());
	ui->spinBox_2->setValue(cfg.maxDi());
	ui->spinBox_3->setValue(cfg.ph());
	auto rob = cfgrw->GetRobotCfg();
	ui->lineEdit_3->setText(rob.id());
	m_plateStackBase->Init(cfg.base());
}
void SystemMainWid::on_pushButton_2_clicked()
{
	PlateStackCfg cfg = {};
	cfg.valied = ui->checkBox->isChecked() ? 1 : 0;
	cfg.id() = ui->lineEdit_2->text();
	cfg.minDi = ui->spinBox->value();
	cfg.maxDi = ui->spinBox_2->value();
	cfg.rob = ui->lineEdit_3->text();
	cfg.ph = ui->spinBox_3->value();
	cfg.base = m_plateStackBase->GetPositon();
	if (!cfgrw->UpdatePlateStackCfg(cfg)) {
		QMessageBox::warning(this, "ERROR", "保存失败");
	}
}

void SystemMainWid::InitUnstackPlate()
{
	auto ids = cfgrw->GetAllUnstackPlateID();
	for (auto& id : ids) {
		auto cfg = cfgrw->GetUnstackPlateCfgByID(id);
		auto temp = new UnstackPlateSettingWid(this);
		temp->Init(cfg);
		ui->tabWidget_3->addTab(temp, id);
	}
}


void SystemMainWid::on_pushButton_stack_add_clicked()
{
	ParamAddDialog dlg({ "码垛托盘编号" }, this);
	dlg.exec();
	auto param = dlg.GetParam();
	if (param.size() != 1) {
		QMessageBox::warning(this, "ERROR", "码垛托盘编号不能为空");
		return;
	}
	QString name = param.at(0);
	auto tmp = new StackPlateSettingWid(this);
	StackPlateCfg cfg = {};
	cfg.id = name;
	tmp->Init(cfg);
	int idx = ui->tabWidget_2->addTab(tmp, name);
	ui->tabWidget_2->setCurrentIndex(idx);
}
void SystemMainWid::on_tabWidget_2_tabCloseRequested(int index)
{
	auto ret = QMessageBox::question(this, "QUESTION", QString("确认删除%1, 删除后数据无法恢复").
		arg(ui->tabWidget_2->tabText(index)));
	if (ret == QMessageBox::StandardButton::Yes) {
		ui->tabWidget_2->widget(index)->deleteLater();
		ui->tabWidget_2->removeTab(index);
	}
}
void SystemMainWid::on_pushButton_stack_save_clicked()
{
	QList<StackPlateCfg> cfgs;
	for (int i = 0; i < ui->tabWidget_2->count(); i++) {
		auto wid = qobject_cast<StackPlateSettingWid*>(ui->tabWidget_2->widget(i));
		cfgs.append(wid->GetCfg());
	}
	if (cfgrw->SetAllStackPlateCfg(cfgs)) {
		for (int i = 0; i < ui->tabWidget_2->count(); i++) {
			ui->tabWidget_2->widget(i)->deleteLater();
		}
		ui->tabWidget_2->clear();
		InitStackPlate();
	} else {
		QMessageBox::warning(this, "ERROR", "保存失败");
	}
}

void SystemMainWid::on_pushButton__unstack_add_clicked()
{
	ParamAddDialog dlg({ "拆垛托盘编号" }, this);
	dlg.exec();
	auto param = dlg.GetParam();
	if (param.size() != 1) {
		QMessageBox::warning(this, "ERROR", "拆垛托盘编号不能为空");
		return;
	}
	QString name = param.at(0);
	auto tmp = new UnstackPlateSettingWid(this);
	UnstackPlateCfg cfg = {};
	cfg.id = name;
	tmp->Init(cfg);
	int idx = ui->tabWidget_3->addTab(tmp, name);
	ui->tabWidget_3->setCurrentIndex(idx);
}
void SystemMainWid::on_tabWidget_3_tabCloseRequested(int index)
{
	auto ret = QMessageBox::question(this, "QUESTION", QString("确认删除%1, 删除后数据无法恢复").
		arg(ui->tabWidget_3->tabText(index)));
	if (ret == QMessageBox::StandardButton::Yes) {
		ui->tabWidget_3->widget(index)->deleteLater();
		ui->tabWidget_3->removeTab(index);
	}
}
void SystemMainWid::on_pushButton_unstack_save_clicked()
{
	QList<UnstackPlateCfg> cfgs;
	for (int i = 0; i < ui->tabWidget_3->count(); i++) {
		auto wid = qobject_cast<UnstackPlateSettingWid*>(ui->tabWidget_3->widget(i));
		cfgs.append(wid->GetCfg());
	}
	if (cfgrw->SetAllUnstackPlateCfg(cfgs)) {
		for (int i = 0; i < ui->tabWidget_3->count(); i++) {
			ui->tabWidget_3->widget(i)->deleteLater();
		}
		ui->tabWidget_3->clear();
		InitUnstackPlate
		();
	} else {
		QMessageBox::warning(this, "ERROR", "保存失败");
	}
}
/*输送线*/
void SystemMainWid::on_tabWidget_4_tabCloseRequested(int index)
{
	auto ret = QMessageBox::question(this, "QUESTION", QString("确认删除%1, 删除后数据无法恢复").
		arg(ui->tabWidget_4->tabText(index)));
	if (ret == QMessageBox::StandardButton::Yes) {
		ui->tabWidget_4->widget(index)->deleteLater();
		ui->tabWidget_4->removeTab(index);
	}
}
void SystemMainWid::on_pushButton_boxline_add_clicked()
{
	ParamAddDialog dlg({ "输送线编号" }, this);
	dlg.exec();
	auto param = dlg.GetParam();
	if (param.size() != 1) {
		QMessageBox::warning(this, "ERROR", "输送线编号不能为空");
		return;
	}
	QString name = param.at(0);
	auto tmp = new BoxlineSettingWid(this);
	BoxlineCfg cfg = {};
	cfg.id = name;
	cfg.posid = 0;
	tmp->Init(cfg);
	int idx = ui->tabWidget_4->addTab(tmp, name);
	ui->tabWidget_4->setCurrentIndex(idx);
}

void SystemMainWid::on_pushButton_boxline_save_clicked()
{
	QList<BoxlineCfg> cfgs;
	for (int i = 0; i < ui->tabWidget_4->count(); i++) {
		auto wid = qobject_cast<BoxlineSettingWid*>(ui->tabWidget_4->widget(i));
		cfgs.append(wid->GetCfg());
	}
	if (cfgrw->SetAllBoxlineCfg(cfgs)) {
		for (int i = 0; i < ui->tabWidget_4->count(); i++) {
			ui->tabWidget_4->widget(i)->deleteLater();
		}
		ui->tabWidget_4->clear();
		InitBoxline();
	} else {
		QMessageBox::warning(this, "ERROR", "业务线保存失败");
	}
}
/**************************业务线相关*********************************/
void SystemMainWid::InitPipeline()
{
	QStringList ids = cfgrw->GetAllPipelineID();

	for (auto& id : ids) {
		auto cfg = cfgrw->GetPipelineCfgByID(id);
		auto tmp = new PipelineSettingWid(this);
		tmp->Init(cfg);
		ui->tabWidget_pipe->addTab(tmp, id);
	}
}


void SystemMainWid::on_pushButton_add_pipe_clicked()
{
	ParamAddDialog dlg({ "业务线名称" }, this);
	dlg.exec();
	auto param = dlg.GetParam();
	if (param.size() != 1) {
		QMessageBox::warning(this, "ERROR", "业务线名称不能为空");
		return;
	}
	QString name = param.at(0);
	auto tmp = new PipelineSettingWid(this);
	PipelineCfg cfg = {};
	cfg.id = name;
	tmp->Init(cfg);
	int idx = ui->tabWidget_pipe->addTab(tmp, name);
	ui->tabWidget_pipe->setCurrentIndex(idx);
}

void SystemMainWid::on_tabWidget_pipe_tabCloseRequested(int index)
{
	auto ret = QMessageBox::question(this, "QUESTION", QString("确认删除%1, 删除后数据无法恢复").
		arg(ui->tabWidget_pipe->tabText(index)));
	if (ret == QMessageBox::StandardButton::Yes) {
		ui->tabWidget_pipe->widget(index)->deleteLater();
		ui->tabWidget_pipe->removeTab(index);
	}
}


void SystemMainWid::on_pushButton_save_pipe_clicked()
{
	QList<PipelineCfg> cfg;
	for (int i = 0; i < ui->tabWidget_pipe->count(); i++) {
		PipelineSettingWid* temp = qobject_cast<PipelineSettingWid*>(ui->tabWidget_pipe->widget(i));
		PipelineCfg cf = temp->GetCfg();
		cfg.append(cf);
	}

	if (cfgrw->SetAllPipelineCfg(cfg)) {
		for (int i = 0; i < ui->tabWidget_pipe->count(); i++) {
			ui->tabWidget_pipe->widget(i)->deleteLater();
		}
		ui->tabWidget_pipe->clear();
		InitPipeline();
	} else {
		QMessageBox::warning(this, "ERROR", "业务线保存失败");
	}

}
